<template>
  <div 
    v-if="visible"
    :class="[
      'bx-alert',
      'bx-glass',
      `bx-alert--${type}`,
      {
        'bx-alert--closable': closable,
        'bx-alert--with-icon': showIcon
      }
    ]"
  >
    <div v-if="showIcon" class="bx-alert__icon">
      <BxIcon :name="iconName" />
    </div>
    
    <div class="bx-alert__content">
      <div v-if="title || $slots.title" class="bx-alert__title">
        <slot name="title">{{ title }}</slot>
      </div>
      <div v-if="$slots.default || message" class="bx-alert__message">
        <slot>{{ message }}</slot>
      </div>
    </div>
    
    <div v-if="closable" class="bx-alert__close" @click="handleClose">
      <BxIcon name="close" />
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import BxIcon from '../basic/BxIcon.vue'

export default {
  name: 'BxAlert',
  components: {
    BxIcon
  },
  props: {
    type: {
      type: String,
      default: 'info',
      validator: (value) => ['success', 'info', 'warning', 'error'].includes(value)
    },
    title: {
      type: String,
      default: ''
    },
    message: {
      type: String,
      default: ''
    },
    closable: {
      type: Boolean,
      default: false
    },
    showIcon: {
      type: Boolean,
      default: true
    },
    icon: {
      type: String,
      default: ''
    }
  },
  emits: ['close'],
  setup(props, { emit }) {
    const visible = ref(true)

    const iconName = computed(() => {
      if (props.icon) return props.icon
      
      const iconMap = {
        success: 'check-circle',
        info: 'info-circle',
        warning: 'alert-triangle',
        error: 'x-circle'
      }
      
      return iconMap[props.type] || 'info-circle'
    })

    const handleClose = () => {
      visible.value = false
      emit('close')
    }

    return {
      visible,
      iconName,
      handleClose
    }
  }
}
</script>

<style scoped>
.bx-alert {
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: var(--bx-spacing-md);
  padding: var(--bx-spacing-md);
  border: 1px solid var(--bx-glass-border);
  border-radius: var(--bx-radius-medium);
  background: var(--bx-glass-bg);
  backdrop-filter: var(--bx-glass-blur);
  font-family: var(--bx-font-family);
  transition: all var(--bx-transition-medium);
}

.bx-alert::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(
    135deg,
    rgba(255, 255, 255, 0.1) 0%,
    rgba(255, 255, 255, 0.05) 100%
  );
  border-radius: inherit;
  pointer-events: none;
  opacity: 0.6;
}

/* 类型样式 */
.bx-alert--success {
  background: rgba(52, 199, 89, 0.15);
  border-color: rgba(52, 199, 89, 0.3);
  color: var(--bx-text-primary);
}

.bx-alert--info {
  background: rgba(0, 122, 255, 0.15);
  border-color: rgba(0, 122, 255, 0.3);
  color: var(--bx-text-primary);
}

.bx-alert--warning {
  background: rgba(255, 149, 0, 0.15);
  border-color: rgba(255, 149, 0, 0.3);
  color: var(--bx-text-primary);
}

.bx-alert--error {
  background: rgba(255, 59, 48, 0.15);
  border-color: rgba(255, 59, 48, 0.3);
  color: var(--bx-text-primary);
}

.bx-alert__icon {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  color: var(--bx-text-primary);
  margin-top: 2px;
}

.bx-alert--success .bx-alert__icon {
  color: rgba(52, 199, 89, 0.9);
}

.bx-alert--info .bx-alert__icon {
  color: rgba(0, 122, 255, 0.9);
}

.bx-alert--warning .bx-alert__icon {
  color: rgba(255, 149, 0, 0.9);
}

.bx-alert--error .bx-alert__icon {
  color: rgba(255, 59, 48, 0.9);
}

.bx-alert__content {
  flex: 1;
  min-width: 0;
}

.bx-alert__title {
  font-size: 16px;
  font-weight: 600;
  line-height: 1.4;
  color: var(--bx-text-primary);
  margin-bottom: 4px;
}

.bx-alert__message {
  font-size: 14px;
  line-height: 1.5;
  color: var(--bx-text-secondary);
}

.bx-alert--with-icon .bx-alert__title {
  margin-top: -2px;
}

.bx-alert__close {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  cursor: pointer;
  color: var(--bx-text-tertiary);
  transition: color var(--bx-transition-medium);
  margin-top: 2px;
}

.bx-alert__close:hover {
  color: var(--bx-text-primary);
}

/* 动画效果 */
.bx-alert {
  animation: bx-alert-slide-in 0.3s ease-out;
}

@keyframes bx-alert-slide-in {
  0% {
    opacity: 0;
    transform: translateY(-10px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .bx-alert {
    padding: var(--bx-spacing-sm);
    gap: var(--bx-spacing-sm);
  }
  
  .bx-alert__icon {
    font-size: 14px;
  }
  
  .bx-alert__title {
    font-size: 15px;
  }
  
  .bx-alert__message {
    font-size: 13px;
  }
  
  .bx-alert__close {
    width: 18px;
    height: 18px;
  }
}
</style>

